﻿using BepInEx;
using CasselGames.Input;
using HarmonyLib;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using MEC;
using Newtonsoft.Json;
using CasselGames.Diplomatic.Asset;
using CasselGames.Diplomatic;
using Spine;
using Spine.Unity;
using CasselGames.Data;
namespace BetterBlackTrading {
    [BepInPlugin("otherCountryCitizen", "OtherCountryCitizen", "1.0.0")]
    public class Plugin: BaseUnityPlugin {

        private void Awake() {
            Harmony.CreateAndPatchAll(typeof(Plugin));
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(CCMake_Info), "MakeSkinInfo")]
        private static bool CCMake_Info_MakeSkinInfo_Prefix(CCMake_Info __instance) {
            __instance.SkinInfo = GetCountrySkin(Gender.Male, "Prayja",0);
            return false;
        }

        private static Sp_SkinInfo GetCountrySkin(Gender _gender, string _country, int _tier) {
            Sp_SkinInfo sp_SkinInfo = new Sp_SkinInfo();
            sp_SkinInfo.Init();
            sp_SkinInfo.m_Gender = _gender;
            sp_SkinInfo.m_UnitKind = UnitKind.Citizen;
            sp_SkinInfo = SpineDresserMgr.Instance.AssembleDefaultSkin(sp_SkinInfo);
            sp_SkinInfo = SpineDresserMgr.Instance.AssembleData("Jobless_1_1", sp_SkinInfo, true);

            DiplomaticMgr diplomaticMgr = GameMgr.Instance.DiplomaticMgr;
            var a = (DiplomaticAsset)Traverse.Create(diplomaticMgr).Property("Asset").GetValue();
            foreach (var item in a.MilitaryAsset.Arr) {
                Debug.Log($"Country Name:{item.CountryName},Tier:{item.Tier}");
            }
            DiplomaticCountryMilitaryData countryMilitaryDataOrNull = diplomaticMgr.GetCountryMilitaryDataOrNull(a.MilitaryAsset.Arr[0].CountryName, a.MilitaryAsset.Arr[0].Tier);
            
            if (countryMilitaryDataOrNull != null) {
                Skin skin = countryMilitaryDataOrNull.GetSkin(sp_SkinInfo.m_SkeletonData, true);
                sp_SkinInfo.SkinSet(skin, sp_SkinInfo.m_SkeletonData);
            } else {
                Debug.LogError("国家信息错误");
            }
            return sp_SkinInfo;
        }


    }
}
